from django.views.generic import ListView
from ModelView.models import *

class ProductList(ListView):
    context_object_name  = 'type_list' # type_list 被传递到模板中
    template_name = "index_view.html" # 渲染模板
    # 查询数据
    queryset = Product.objects.values("type").distinct()

    # 重写queryset 功能与queryset实现功能一致
    def get_queryset(self):
        # 打印URL的变量id
        #print(self.kwargs['id'])
        # 打印URL的参数name
        #print(self.kwargs['name'])
        # 获取请求方式
        #print(self.request.method)
        type_list = Product.objects.values("type").distinct()
        #print(type_list)
        return type_list

    # 添加其他变量  设置html模板其他变量
    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context['name_list'] = Product.objects.values("type","name")
        context['title'] = "天天生鲜-首页"
        context['strs'] = "abc"
        #print("context",context)
        return context